-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local Widget = require "widgets/widget"
    local Image = require "widgets/image" -- 引入image控件
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 坐标记录
    local function GetHUDLoation()
        local data = TUNING.LORAMIA_FN:Get_ThePlayer_Cross_Archived_Data("laser_cannon_hud_location")
        if data == nil then
            return 0.5,0.5
        else
            return data.x,data.y
        end
    end
    local function SetHUDLoation(x,y)
        TUNING.LORAMIA_FN:Set_ThePlayer_Cross_Archived_Data("laser_cannon_hud_location",{x = x,y = y})
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local create_hud_box = function(inst,indicator)
    --------------------------------------------------------------------------
    --- 天赋检查
        if not inst:IsPlayerUnlockedTalent(ThePlayer) then
            return
        end
    --------------------------------------------------------------------------
    --- 前置根节点
        local front_root = ThePlayer.HUD
        local atlas = "images/widgets/loramia_laser_cannon_hud.xml"
    --------------------------------------------------------------------------
    --- 创建根节点
        local root = front_root:AddChild(Widget())
        root:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
        root:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
        root:SetPosition(1000,500)
        root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
    --------------------------------------------------------------------------
    -------- 启动坐标跟随缩放循环任务，缩放的时候去到指定位置。官方好像没预留这类API，或者暂时找不到方法
        function root:LocationScaleFix()
            if self.x_percent and not self.__mouse_holding  then
                local scrnw, scrnh = TheSim:GetScreenSize()
                if self.____last_scrnh ~= scrnh then
                    local tarX = self.x_percent * scrnw
                    local tarY = self.y_percent * scrnh
                    self:SetPosition(tarX,tarY)
                end
                self.____last_scrnh = scrnh
            end
        end
        
        root.x_percent,root.y_percent = GetHUDLoation()
        root:LocationScaleFix()    
        root.inst:DoPeriodicTask(2,function()
            root:LocationScaleFix()
        end)
    --------------------------------------------------------------------------
    ---- 鼠标拖动
        local old_OnMouseButton = root.OnMouseButton
        root.OnMouseButton = function(self,button, down, x, y)
            if down and button == MOUSEBUTTON_RIGHT then

                if not root.__mouse_holding  then
                    root.__mouse_holding = true      --- 上锁
                        --------- 添加鼠标移动监听任务
                        root.___follow_mouse_event = TheInput:AddMoveHandler(function(x, y)  
                            root:SetPosition(x,y,0)
                        end)
                        --------- 添加鼠标按钮监听
                        root.___mouse_button_up_event = TheInput:AddMouseButtonHandler(function(button, down, x, y) 
                            if button == MOUSEBUTTON_RIGHT and down == false then    ---- 左键被抬起来了
                                root.___mouse_button_up_event:Remove()       ---- 清掉监听
                                root.___mouse_button_up_event = nil

                                root.___follow_mouse_event:Remove()          ---- 清掉监听
                                root.___follow_mouse_event = nil

                                root:SetPosition(x,y,0)                      ---- 设置坐标
                                root.__mouse_holding = false                 ---- 解锁

                                local scrnw, scrnh = TheSim:GetScreenSize()
                                root.x_percent = x/scrnw
                                root.y_percent = y/scrnh

                                -- owner:PushEvent("loramia_wellness_bars.save_cmd",{    --- 发送储存坐标。
                                --     pt = {x_percent = root.x_percent,y_percent = root.y_percent},
                                -- })
                                SetHUDLoation(root.x_percent,root.y_percent)

                            end
                        end)
                end

            end
            return old_OnMouseButton(self,button, down, x, y)
        end
    --------------------------------------------------------------------------
    --- 移除事件
        indicator:ListenForEvent("onremove",function()
            root:Kill()
        end)
    --------------------------------------------------------------------------
    --- box节点
        local MainScale = 0.6
        local box = root:AddChild(Image(atlas,"box.tex"))
        box:SetScale(MainScale, MainScale,MainScale)
    --------------------------------------------------------------------------
    --- 装饰线
        box:AddChild(Image(atlas,"grayline.tex")):SetPosition(0,50)
        -- box:AddChild(Image(atlas,"grayline.tex")):SetPosition(0,20)
        -- box:AddChild(Image(atlas,"grayline.tex")):SetPosition(0,-20)
        -- box:AddChild(Image(atlas,"grayline.tex")):SetPosition(0,-60)
    --------------------------------------------------------------------------
    --- txt 参数
        local txt_font = CODEFONT
        local txt_size = 40
        local txt_color = {50/255,50/255,50/255,1}
    --------------------------------------------------------------------------
    --- 图标
        local overload_icon = box:AddChild(Image(atlas,"overload.tex"))
        overload_icon:SetPosition(-80,0)
        local overload_txt = box:AddChild(Text(txt_font,txt_size,"100/100",txt_color))
        overload_txt:SetPosition(30,0)
    --------------------------------------------------------------------------
    --- 图标
        local battery_icon = box:AddChild(Image(atlas,"battery.tex"))
        battery_icon:SetPosition(-80,-45)
        battery_icon:SetRotation(55)
        local battery_txt = box:AddChild(Text(txt_font,txt_size,"1000/1000",txt_color))
        battery_txt:SetPosition(30,-45)
    --------------------------------------------------------------------------
    --- 图标
        local laser_cannon_icon = box:AddChild(Image(atlas,"gun.tex"))
        laser_cannon_icon:SetPosition(-100,60)
        laser_cannon_icon:SetScale(.8,.8,.8)
    --------------------------------------------------------------------------
    --- 更新
        local function update_value()
            local battery = inst.Power and inst.Power:Battery_Get() or 0
            local battery_max = 1000
            local overload = inst.Power and inst.Power:Overload_Get() or 0
            local overload_max = 100
            --- 位数不够的时候补全前面空格
            local battery_str = string.format("%d/%d",battery,battery_max)
            local overload_str = string.format("%d/%d",overload,overload_max)
            ---- 补全空格，够max位数
            battery_str = string.format("%-"..#tostring(battery_max)..".0f/%d",battery,battery_max)
            overload_str = string.format("%-"..#tostring(overload_max)..".0f/%d",overload,overload_max)                    
            ---- 设置文本
            battery_txt:SetString(battery_str)
            overload_txt:SetString(overload_str)
        end
        update_value()
        box.inst:ListenForEvent("power_data_update",update_value,inst)
    --------------------------------------------------------------------------
    --- 关闭(隐藏）按钮
        local close_button = root:AddChild(ImageButton(
            "images/global_redux.xml",
            "shop_sale_tag.tex",
            "shop_sale_tag.tex",
            "shop_sale_tag.tex",
            "shop_sale_tag.tex",
            "shop_sale_tag.tex"
        ))
        close_button:SetPosition(65,30,0)
        local temp_x = close_button.image:AddChild(Text(txt_font,70,"X",txt_color))
        temp_x:SetPosition(20,20,0)
        local scale = 0.2
        close_button:SetScale(scale,scale,scale)
        local function box_show_hide()
            if inst.WIDGET_BOX_SHOWING == true then
                box:Hide()
                inst.WIDGET_BOX_SHOWING  = false
            else
                box:Show()
                inst.WIDGET_BOX_SHOWING  = true
            end
        end
        if inst.WIDGET_BOX_SHOWING == nil then
            box_show_hide()
        elseif inst.WIDGET_BOX_SHOWING == false then
            box:Hide()
        else
            box:Show()
        end
        close_button:SetOnClick(box_show_hide)
    --------------------------------------------------------------------------
    ---
        return root
    --------------------------------------------------------------------------
end


return function(inst,indicator)
    local hud = create_hud_box(inst,indicator)
    indicator:DoPeriodicTask(3,function()
        if hud == nil then
            hud = create_hud_box(inst,indicator)
        end
    end)
end